package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Autowired
    private OrderSettingListDao orderSettingListDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Map> maps = orderSettingListDao.findByCondition(queryString);
        List<Map> result = maps.getResult();
        for (Map map : result) {
            Date date = (Date) map.get("orderDate");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            map.put("orderDate",format);
            System.out.println(map.get("orderDate"));
        }
        return new PageResult(maps.getTotal(),result);
    }

    // 修改预约的状态
    public void updateStatusByOrderId(int orderId) throws Exception {
        // 判断是否已经预约
        Order byOrderId = orderSettingListDao.findByOrderId(orderId);
        if(byOrderId != null && !Order.ORDERSTATUS_YES.equals(byOrderId.getOrderStatus())){
            // 更新预约状态
            orderSettingListDao.updateStatusByOrderId(orderId);
        }else{
            throw new Exception("抛出异常");        // 修改失败（不存在、已经预约）父方法捕获
        }
    }

    // 删除一条预约信息
    public void delete(Map map) throws Exception {
        // 判断是否已经预约确珍过了
        if(Order.ORDERSTATUS_YES.equals(map.get("orderStatus"))){
            throw new Exception("抛出异常");        // 修改失败（不存在、已经预约）父方法捕获
        }

        // 修改预约日期的人数 -1
        System.out.println(DateUtils.parseString2Date((String)map.get("orderDate")));
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date((String)map.get("orderDate")));
        orderSetting.setReservations(orderSetting.getReservations() - 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);                      // 将数据写回数据库

        orderSettingListDao.deleteByOrderId(Integer.parseInt(map.get("id").toString()));   // 删除预约信息
    }
}
